package com.example.demo.controller;

import com.example.demo.dao.DepartmentMapper;
import com.example.demo.dto.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api")
public class StreamController {

    private static int[] ages = {13, 23, 32, 32, 42, 54, 65, 76};
    private static Collection collection = new ArrayList();
    static {
        collection.add("test1");
        collection.add("test2");
        collection.add("test3");
    }

    @Autowired
    DepartmentMapper departmentMapper;

    /**
     * jdk8
     * ::
     * 对象::实例方法名
     * 类::静态方法
     * 类::实例方法
     */

    // 过滤
    @GetMapping("/filter")
    public IntStream testFilter() {
        return Arrays.stream(ages).filter(e -> e > 40);
    }

    // 去重
    @GetMapping("/distinct")
    public IntStream testDistinct() {
        return Arrays.stream(ages).distinct();
    }

    // 限制
    @GetMapping("/limit")
    public IntStream testLimit() {
        return Arrays.stream(ages).limit(4);
    }

    // 跳过前几个
    @GetMapping("/skip")
    public IntStream testSkip() {
        return Arrays.stream(ages).skip(3);
    }

    // map
    @GetMapping("/map")
    public List<String> testMap() {
        return departmentMapper.getDepartmentDataList().stream().map(Department::getDepartName).collect(Collectors.toList());
    }

    /**
     * boxed 返回由该流的元素组成的Stream流
     * @return
     */
    // mapToDouble
    @GetMapping("/mapToDouble")
    public List<Double> testMapToDouble() {
        return departmentMapper.getDepartmentDataList().stream().mapToDouble(e -> e.getId() * 4).boxed().collect(Collectors.toList());
    }

    // mapToInt
    @GetMapping("/mapToInt")
    public Integer testMapToInt() {
        return departmentMapper.getDepartmentDataList().stream().mapToInt(e -> e.getId().compareTo(2)).sum();
    }

    // flatMap
    @GetMapping("/flatMap")
    public List<String> testFlatMap() {
        List<List<String>> database = new ArrayList<>();
        List<String> noSql = Arrays.asList("redis", "hbase", "membercache");
        List<String> sql = Arrays.asList("mysql", "oracle", "db2");
        database.add(noSql);
        database.add(sql);

        return database.stream().flatMap(s -> s.stream().map(String::toUpperCase)).collect(Collectors.toList());
    }

    /**
     * Collection 是Java中一个最顶层集合之一，下有
     * List Queue Set  在下面是一些抽象类，最后具体实现类通常有
     * ArrayList LinkedList HashSet LinkedHashSet ArrayBlockingQueue   接口方法
     * .add / .addAll / .clear / .contains / .containAll / isEmpty / .remove / .removeAll / retainAll / .size / toArray
     */
    @GetMapping("/collection")
    public Collection testCollection() {
        return collection;
    }

    // HashSet
    @GetMapping("hashSet")
    public Collection<String> testHashSet() {
        Collection<String> collectStr = new HashSet<>();
        collectStr.add("hashSet1");
        collectStr.add("hashSet2");
        collection.add(collectStr);
        return collectStr;
    }

    // remove 移除
    @GetMapping("/remove")
    public Collection testRemove() {
        collection.remove("test1");
        return collection;
    }

    // 随机数
    @PostMapping("/random")
    public Integer testRandom(@RequestBody String str) {
        System.out.print(str);
        Random random = new Random();
        return random.nextInt(100);
    }

}
